$(function () {
    getUserInfo()
    //退出登录
    $('#logout').on('click', function () {
        layer.confirm('是否确认退出登录？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //退出登录:强行跳转到登录页,销毁token
            location.href = '/login.html'
            locationbar.removeItem('token')
            layer.close(index)
        });
    })
})
//封装用户信息
function getUserInfo() {
    axios({
        url: '/my/userinfo',
        method: 'get',
    }).then(res => {
        if (res.data.status != 0) {
            return layui.layer.msg(res.data.message)
        }
        renderAvatar(res.data.data)
    });
}
//封装渲染用户信息
function renderAvatar(user) {
    // 渲染欢迎词; 优先渲染nickname,没有才渲染username
    let name = user.nickname || user.username
    $('.welcome').html('欢迎&nbsp' + name)
    if (user.user_pic != null) {
        $(".layui-nav-img").show().attr('src', user.user_pic);
        $(".avatar-text").hide();
    } else {
        $(".layui-nav-img").hide();
        let first = name[0].toUpperCase();
        $(".avatar-text").show().html(first);
    }
}